package com.hcqc.demo.controller;

import com.hcqc.demo.pojo.User;
import com.hcqc.demo.service.UserServiceImpl;
import com.hcqc.demo.vo.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class TestController {

    @Autowired
    UserServiceImpl userService;


    @RequestMapping("exportExcel")
    public void export(HttpServletResponse response){
        List<User> userList = userService.queryUserInfo();
        System.out.println(userList);
        //导出操作
        ExcelUtil.exportExcel(userList,"用户信息","sheet1",User.class,"testDATA.xls",response);
    }

    @RequestMapping("importExcel")
    public String importExcel(){
        String filePath = "D:\\test.xls";
        //解析excel，
        List<User> userList = ExcelUtil.importExcel(filePath,1,1,User.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+userList.size()+"】行");

        userService.addUserInfo(userList);
        List<User> userList2 = userService.queryUserInfo();
        return userList2.toString();

    }



}
